﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hostal.Bussines.Repository.Service;
using Hostal.Bussines.Entities;

namespace Hostal.Bussines.Repository
{
    public class InsumoRepository:MasterRepository,IInsumoRepository
    {
        public List<Insumo> GetAllFromInsumo()
        {
            var query = from i in Context.Insumo
                        select i;
            return query.ToList();
        }

        public Insumo GetFromInsumoById(int id)
        {         
            return Context.Insumo.Find(id);
        }

        public List<Insumo> GetFromInsumoByNombre(string nombre)
        {
            var query= from i in Context.Insumo
                           select i;
            if (nombre != null && nombre != "")
            {
                query = from i in query
                        where i.Nombre.ToUpper().Contains(nombre.ToUpper())
                        select i;
            }
            return query.ToList();
        }

        public void AddInsumo(Insumo insumo)
        {
            Context.Insumo.Add(insumo);
            Context.SaveChanges();
        }

        public void UpdateInsumo(Insumo insumo)
        {
            Context.Entry(insumo).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveInsumo(int id)
        {
            var existe = Context.Insumo
                .Where(i =>i.IdInsumos.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                Context.Insumo.Remove(existe);
                Context.SaveChanges();
            }
        }
    }
}
